var _originalNote='';
var _originalCategory='';
var _originalSearchTerms='';

var printNoteExchangeTitle;
var printNoteExchangeBody;
var printNoteExchangeKey;
var printNoteExchangeSearchTerms;
var printNoteExchangeCatKey;
var printNoteExchangeCreateDate;
var printNoteExchangeModDate;

function setupNote()
{
  var textControl=document.getElementById('thundernotenote');
  var docHeight=document.height;
  var docWidth=document.width;
  //textControl.height=docHeight-10;
  //textControl.width=docWidth-10;
  applyConfig(textControl);
  //class="mynote-yl"
  
  textControl.value=opener.getNoteContent();
  _originalNote=opener.getNoteContent();
  textControl.setAttribute('index',opener.getNoteIndex());
  _originalCategory=opener.getNoteCategory();
  _originalSearchTerms=opener.getNoteSearchTerms();
  textControl.focus();
}

function applyConfig(txtCtrl)
{
  var cfg=readConfig();
  var children=cfg.documentElement.childNodes;
  
  for(var x=0;x<children.length;x++)
  {
    if(children[x].nodeName=='NotesWindow')
      applyNoteConfig(children[x],txtCtrl);
  }
}

function applyNoteConfig(node,txtCtrl)
{
  var children=node.childNodes;
  var fontSize='';
  var fontColor='';
  var bkStyle='';
  
  for(var x=0;x<children.length;x++)
  {
    if(children[x].nodeName=='FontSize')
      fontSize=getXMLNodeValue(children[x]);
    if(children[x].nodeName=='FontColour')
      fontColor=getXMLNodeValue(children[x]);
    if(children[x].nodeName=='Style')
      bkStyle=getXMLNodeValue(children[x]);
  }

  if(fontSize==null) fontSize='';
  if(fontColor==null) fontColor='';
  if(bkStyle==null) bkStyle='';
  
  if(fontSize<2 || fontSize>18) fontSize=18;
  
  fontColor=fontColor.toLowerCase();
  if(fontColor!='blue' && fontColor!='black' && fontColor!='green' && fontColor!='red') fontColor='blue';
  
  bkStyle=bkStyle.toLowerCase();
  if(bkStyle!='yellow-lined' && bkStyle!='yellow' && bkStyle!='white-lined' && bkStyle!='white') bkStyle='yellow-lined';    

  if(bkStyle=='yellow')             txtCtrl.setAttribute("class","mynote-y");
  else if(bkStyle=='yellow-lined')  txtCtrl.setAttribute("class","mynote-yl");
  else if(bkStyle=='white')         txtCtrl.setAttribute("class","mynote-w");
  else                              txtCtrl.setAttribute("class","mynote-wl");

  var s="-moz-appearance: none;";
  
  if(bkStyle=='yellow' || bkStyle=='yellow-lined')  s=s+"background-color: #FFFFCC;";
  else                                              s=s+"background-color: #white;";
  
  if(bkStyle=='yellow-lined')                       s=s+"background-image:url(tn-background.png);";
  if(bkStyle=='yellow-white')                       s=s+"background-image:url(tn-background2.png);";

  s=s+"background-repeat:repeat;";
  s=s+"background-attachment:fixed;";
  s=s+"border-width:1;";
  s=s+"border-style:solid;";
  s=s+"border-color:FFFFFF;";
  s=s+"font-family:Arial;";
  s=s+"font-size:"+fontSize+"pt;";
  
  s=s+"color:"+fontColor+";";

  txtCtrl.setAttribute("style",s);

}

function updateNote()
{
  var textControl=document.getElementById('thundernotenote');
  var note=textControl.value;
  var idx=textControl.getAttribute('index');
  var d=new Date();
  var title,body;
  
  note=note.trim();
  // check if note is empty
  if(note!='')
  {
    // check if note has changed
    if(note!=_originalNote)
    {
      title=getTitle(note);
      body=getBody(note);
      if(idx==-1)
      {
        // insert a new note
        idx=d.getTime();
        opener.createNewNote(title,body,idx,_originalCategory,_originalSearchTerms);
      }
      else
      {
        // modify an existing note
        opener.modifyExistingNote(title,body,idx,_originalCategory,_originalSearchTerms,true);
      }
    }
  }
}

function callPrintDialog()
{
  var textControl=document.getElementById('thundernotenote');
  var note=textControl.value;
  var idx=textControl.getAttribute('index');
  var title,body;
  
  note=note.trim();
  title=getTitle(note);
  body=getBody(note).trim();

  printNoteExchangeTitle=title;
  printNoteExchangeBody=body.replace(/\r\n/g,'<br>');
  printNoteExchangeBody=printNoteExchangeBody.replace(/\r/g,'<br>');
  printNoteExchangeBody=printNoteExchangeBody.replace(/\n/g,'<br>');
  printNoteExchangeKey=idx;
  printNoteExchangeSearchTerms=_originalSearchTerms;
  printNoteExchangeCatKey=_originalCategory;
  printNoteExchangeCreateDate='';
  printNoteExchangeModDate='';
  var newwin=window.open(_ThunderNotePrintXUL,'','chrome,width=675,height=600,resizable=yes');
}

function onNoteKeyPress(e)
{
  // courtesy of http://users.fmg.uva.nl/rgrasman/jscript/2005/07/capturing-escape-esc-key-in-javascript.html
  var kC  = (window.event) ?    // MSIE or Firefox?
             event.keyCode : e.keyCode;
  var Esc = (window.event) ?   
            27 : e.DOM_VK_ESCAPE // MSIE : Firefox
  if(kC==Esc)
  {
     updateNote();
     window.close();
  }
  
  return(true);
}

function getPrintTitle(){return(printNoteExchangeTitle);}
function getPrintBody(){return(printNoteExchangeBody);}
function getPrintKey(){return(printNoteExchangeKey);}
function getPrintSearchTerms(){return(printNoteExchangeSearchTerms);}
function getPrintCatKey(){return(printNoteExchangeCatKey);}
function getPrintCreateDate(){return(printNoteExchangeCreateDate);}
function getPrintModDate(){return(printNoteExchangeModDate);}


